import asyncio
import logging
import os
import queue
import random
import re
import threading
from datetime import datetime

import cv2
from PIL import Image, ImageDraw, ImageFilter, ImageFont
from aiogram import types
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup
from aiogram.types import ParseMode
from rembg import remove as remove_bg

import client
import client_kb
from create_bot import dp
from data.Logger import CustomLoggerAdapter
from .kz_country_menu import create_kz
from .kz_pass_generate import kz_pass_generate

logger = logging.getLogger(__name__)
logger = CustomLoggerAdapter(logger, "Kazakhstan", "Passport")


def get_jsn(data):
    discharge_7 = None
    if data["date_of_birth"][6] + data["date_of_birth"][7] == "20":
        if data["sex"] == "M":
            discharge_7 = "5"
        if data["sex"] == "F":
            discharge_7 = "6"
    elif data["date_of_birth"][6] + data["date_of_birth"][7] == "19":
        if data["sex"] == "M":
            discharge_7 = "3"
        if data["sex"] == "F":
            discharge_7 = "4"
    else:
        discharge_7 = str(random.randint(0, 6))

    jsn = {
        1: data["date_of_birth"][8],
        2: data["date_of_birth"][9],
        3: data["date_of_birth"][3],
        4: data["date_of_birth"][4],
        5: data["date_of_birth"][0],
        6: data["date_of_birth"][1],
        7: discharge_7,
        8: str(random.randint(0, 9)),
        9: str(random.randint(0, 9)),
        10: str(random.randint(0, 9)),
        11: str(random.randint(0, 9)),
    }
    cntr = 0
    for i in jsn:
        cntr += int(jsn[i])
    discharge_12 = cntr % 11
    jsn[12] = str(discharge_12)

    jsn_str = ""
    for i in jsn:
        jsn_str += jsn[i]
    return jsn_str


def process_generate_signature(user_id, data):
    """Генерация подписи."""
    text = data.capitalize()
    txt = Image.new("RGBA", (1900, 600), (255, 255, 255, 0))
    go_txt = ImageDraw.Draw(txt)
    fnt1 = ImageFont.truetype("./fonts/1_ru.ttf", size=270)
    go_txt.text(xy=(200, 50), text=f"{text}", font=fnt1, fill="#000000")
    fnt2 = ImageFont.truetype("./fonts/2_ru.ttf", size=130)
    go_txt.text(xy=(800, 50), text=f"{text}", font=fnt2, fill="#000000", stroke_width=1)
    fnt3 = ImageFont.truetype("./fonts/3_ru.ttf", size=180)
    go_txt.text(
        xy=(1400, 50), text=f"{text}", font=fnt3, fill="#000000", stroke_width=1
    )
    fnt4 = ImageFont.truetype("./fonts/4_ru.otf", size=100)
    go_txt.text(
        xy=(200, 400), text=f"{text}", font=fnt4, fill="#000000", stroke_width=1
    )
    fnt5 = ImageFont.truetype("./fonts/5_ru.ttf", size=150)
    go_txt.text(
        xy=(800, 400), text=f"{text}", font=fnt5, fill="#000000", stroke_width=1
    )
    fnt6 = ImageFont.truetype("./fonts/6_ru.ttf", size=80)
    go_txt.text(xy=(1400, 400), text=f"{text}", font=fnt6, fill="#000000")
    fnt66 = ImageFont.truetype("./fonts/66.ttf", size=80)
    go_txt.text(xy=(1430, 440), text=f"k", font=fnt66, fill="#000000")
    txt_blur = txt.filter(ImageFilter.GaussianBlur(radius=1))
    holst = Image.new("RGBA", (1900, 600), (255, 255, 255, 255))
    choose_signature = Image.alpha_composite(holst, txt_blur)
    choose_signature.save(
        f"./files/kz/kz_passport/photo_usr/{user_id}_choose_signature.png"
    )


def process_passport_photo(user_id):
    try:
        # Load the image and face detection classifier
        img = cv2.imread(
            f"./files/kz/kz_passport/photo_usr/{user_id}.png", cv2.IMREAD_UNCHANGED
        )
        face_cascade = cv2.CascadeClassifier(
            "./data/haarcascade_frontalface_default.xml"
        )

        # Detect faces in the image
        faces = face_cascade.detectMultiScale(img, 1.2, 5)

        # If faces are detected, crop and resize the image
        if len(faces) != 0:
            (x, y, w, h) = faces[0]
            h_top = int(h * 0.4)
            h_bottom = int(h * 0.3)
            w_left = int(w * 0.2)
            w_right = int(w * 0.2)
            x_left = max(0, x - w_left)
            x_right = min(img.shape[1], x + w + w_right)
            w_new = x_right - x_left
            y_top = max(0, y - h_top)
            y_bottom = min(img.shape[0], y + h + h_bottom)
            h_new = y_bottom - y_top

            cropped_img = img[y_top : y_top + h_new, x_left : x_left + w_new]
            cropped_img_pil = Image.fromarray(
                cv2.cvtColor(cropped_img, cv2.COLOR_BGRA2RGBA)
            )

            # Resize the image proportionally to the width of 264px
            target_width = 264
            width_ratio = target_width / cropped_img_pil.width
            new_width = target_width
            new_height = int(cropped_img_pil.height * width_ratio)
            resized_width = cropped_img_pil.resize(
                (new_width, new_height), Image.ANTIALIAS
            )

            # Resize the image proportionally to the height of 345px
            target_height = 320
            height_ratio = target_height / resized_width.height
            new_height = target_height
            new_width = int(resized_width.width * height_ratio)
            resized_height = resized_width.resize(
                (new_width, new_height), Image.ANTIALIAS
            )

            # Center crop the image to get the width of 264px
            left = (resized_height.width - target_width) / 2
            top = 0
            right = (resized_height.width + target_width) / 2
            bottom = new_height
            cropped_resized = resized_height.crop((left, top, right, bottom))

            # Convert the image to grayscale and keep the alpha channel
            # grayscale_img = cropped_resized.convert("LA").convert("RGBA")
            # grayscale_img.putalpha(cropped_resized.getchannel("A"))

            # Save the processed image with the alpha channel
            cropped_resized.save(f"./files/kz/kz_passport/photo_usr/{user_id}_w.png")
            os.remove(f"./files/kz/kz_passport/photo_usr/{user_id}.png")
        else:
            print(f"No faces found in image for user_id {user_id}")
    except Exception as e:
        print(f"Error processing passport photo for user_id {user_id}: {str(e)}")


class FSM_kz_passport(StatesGroup):
    doc_type = State()
    name = State()
    name_en = State()
    last_name = State()
    last_name_en = State()
    date_of_birth = State()
    place_of_birth = State()
    sex = State()
    nationality = State()
    authority = State()
    issue_date = State()
    pass_number = State()
    jsn = State()
    signature = State()
    photo = State()
    size = State()
    edit = State()


nationalities = [
    "ҚАЗАҚ / КАЗАХ",
    "ҚАЗАҚ / КАЗАШКА",
    "ОРЫС / РУССКИЙ",
    "ОРЫС / РУССКАЯ",
    "УКРАИН / УКРАИНЕЦ",
    "УКРАИН / УКРАИНКА",
    "БЕЛОРУС / БЕЛОРУС",
    "БЕЛОРУС / БЕЛОРУСКА",
]

countries = [
    "afghanistan",
    "albania",
    "algeria",
    "andorra",
    "angola",
    "antigua and barbuda",
    "argentina",
    "armenia",
    "aruba",
    "australia",
    "austria",
    "azerbaijan",
    "bahamas",
    "bahrain",
    "bangladesh",
    "barbados",
    "belarus",
    "belgium",
    "belize",
    "bolivia",
    "bosnia and herzegovina",
    "botswana",
    "brazil",
    "brunei",
    "bulgaria",
    "cambodia",
    "cameroon",
    "canada",
    "cayman islands",
    "chile",
    "china",
    "colombia",
    "congo",
    "costa rica",
    "croatia",
    "cuba",
    "cyprus",
    "czech republic",
    "denmark",
    "dominican republic",
    "ecuador",
    "egypt",
    "el salvador",
    "estonia",
    "faroe islands",
    "finland",
    "france",
    "french polynesia",
    "gabon",
    "georgia",
    "germany",
    "ghana",
    "greece",
    "greenland",
    "guadeloupe",
    "guam",
    "guatemala",
    "guinea",
    "haiti",
    "hashemite kingdom of jordan",
    "honduras",
    "hong kong",
    "hungary",
    "iceland",
    "india",
    "indonesia",
    "iran",
    "iraq",
    "ireland",
    "isle of man",
    "israel",
    "italy",
    "jamaica",
    "japan",
    "kazakhstan",
    "kenya",
    "kosovo",
    "kuwait",
    "latvia",
    "lebanon",
    "libya",
    "liechtenstein",
    "luxembourg",
    "macedonia",
    "madagascar",
    "malaysia",
    "malta",
    "martinique",
    "mauritius",
    "mayotte",
    "mexico",
    "mongolia",
    "montenegro",
    "morocco",
    "mozambique",
    "myanmar [burma]",
    "namibia",
    "nepal",
    "netherlands",
    "new caledonia",
    "new zealand",
    "nicaragua",
    "nigeria",
    "norway",
    "oman",
    "pakistan",
    "palestine",
    "panama",
    "papua new guinea",
    "paraguay",
    "peru",
    "philippines",
    "poland",
    "portugal",
    "puerto rico",
    "republic of korea",
    "republic of lithuania",
    "republic of moldova",
    "romania",
    "russia",
    "saint lucia",
    "san marino",
    "saudi arabia",
    "senegal",
    "serbia",
    "singapore",
    "slovakia",
    "slovenia",
    "south africa",
    "spain",
    "sri lanka",
    "sudan",
    "suriname",
    "swaziland",
    "sweden",
    "switzerland",
    "taiwan",
    "tanzania",
    "thailand",
    "trinidad and tobago",
    "tunisia",
    "turkey",
    "u.s. virgin islands",
    "ukraine",
    "united arab emirates",
    "united kingdom",
    "united states",
    "uruguay",
    "venezuela",
    "vietnam",
    "zambia",
    "zimbabwe",
]

data_fields = [
    {"title": "Имя", "key": "name"},
    {"title": "Имя на английском", "key": "name_en"},
    {"title": "Фамилия", "key": "last_name"},
    {"title": "Фамилия на английском", "key": "last_name_en"},
    {"title": "Дата рождения", "key": "date_of_birth"},
    {"title": "Место рождения", "key": "place_of_birth"},
    {"title": "Пол", "key": "sex"},
    {"title": "Национальность", "key": "nationality"},
    {"title": "Орган выдачи", "key": "authority"},
    {"title": "Дата выдачи", "key": "issue_date"},
    {"title": "Номер бланка паспорта", "key": "pass_number"},
    {"title": "ЖСН", "key": "jsn"},
    {"title": "Подпись", "key": "signature"},
    {"title": "Фото", "key": "photo"},
    {"title": "Размер документа", "key": "size"},
]

male_values = ["мужской", "м", "male", "m"]

female_values = ["женский", "ж", "female", "f"]


async def data_checkup(user_id: int, state: FSMContext):
    try:
        data = await state.get_data()
        checkup_text = "Данные клиента:\n"
        for field in data_fields:
            if field["key"] in data and data[field["key"]] != None:
                checkup_text += f"✅{field['title']}: {data[field['key']]}\n"
            else:
                checkup_text += f"{field['title']}:\n"
        if "message_to_edit" in data:
            await dp.bot.delete_message(
                chat_id=user_id, message_id=data["message_to_edit"]
            )
        return (await dp.bot.send_message(chat_id=user_id, text=checkup_text))[
            "message_id"
        ]
    except:
        pass


async def delete_user_reply(user_id: int, message_id: int):
    try:
        await dp.bot.delete_message(chat_id=user_id, message_id=message_id)
    except Exception as e:
        logger.warning(f"Unable to delete user message: {e}")


async def kz_pass_name_input(message: types.Message, state: FSMContext):
    await state.update_data(id=message.from_user.id)
    await state.update_data(name=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите имя✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_name,
        )
    )["message_id"]
    logger.info(
        f"Пользователь {message.from_user.username} начал заполнение анкеты на паспорт Казахстана."
    )
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.name.set()


@dp.message_handler(state=FSM_kz_passport.name)
async def kz_pass_name_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        await create_kz(message)
    else:
        name = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if re.match("^[А-Яа-я `]+$", message.text) is not None:
            name = message.text
        if name is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите имя в правильном формате.\n"
                    "<i>Формат ввода: только кириллица.</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_name,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(name=name.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_name_en_input(message, state)


async def kz_pass_name_en_input(message: types.Message, state: FSMContext):
    await state.update_data(name_en=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите имя на английском✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_name_en,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.name_en.set()


@dp.message_handler(state=FSM_kz_passport.name_en)
async def kz_pass_name_en_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.doc_type.set()
        await kz_pass_name_input(message, state)
    else:
        name_en = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if re.match("^[A-Za-z `]+$", message.text) is not None:
            name_en = message.text
        if name_en is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите имя на английском в правильном формате.\n"
                    "<i>Формат ввода: только латинские буквы.</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_name_en,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(name_en=name_en.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_last_name_input(message, state)


async def kz_pass_last_name_input(message: types.Message, state: FSMContext):
    await state.update_data(last_name=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите фамилию✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_last_name,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.last_name.set()


@dp.message_handler(state=FSM_kz_passport.last_name)
async def kz_pass_last_name_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.name.set()
        await kz_pass_name_en_input(message, state)
    else:
        last_name = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if re.match("^[А-Яа-я `]+$", message.text) is not None:
            last_name = message.text
        if last_name is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите фамилию в правильном формате.\n"
                    "<i>Формат ввода: только кириллица.</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_last_name,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(last_name=last_name.upper())
            data["sign"] = last_name.capitalize()
            if len(data["sign"]) > 5:
                data["sign"] = data["sign"][: random.randint(3, 5)]
            await state.update_data(sign=data["sign"])
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_last_name_en_input(message, state)


async def kz_pass_last_name_en_input(message: types.Message, state: FSMContext):
    await state.update_data(last_name_en=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите фамилию на английском✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_last_name_en,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.last_name_en.set()


@dp.message_handler(state=FSM_kz_passport.last_name_en)
async def kz_pass_last_name_en_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.name_en.set()
        await kz_pass_last_name_input(message, state)
    else:
        last_name_en = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if re.match("^[A-Za-z `]+$", message.text) is not None:
            last_name_en = message.text
        if last_name_en is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите имя в правильном формате.\n"
                    "<i>Формат ввода: только латинские буквы.</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_last_name_en,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(last_name_en=last_name_en.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_date_of_birth_input(message, state)


async def kz_pass_date_of_birth_input(message: types.Message, state: FSMContext):
    await state.update_data(date_of_birth=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите дату рождения✏️\n" "<i>Формат ввода: дд.мм.гггг</i>",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_date_of_birth,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.date_of_birth.set()


@dp.message_handler(state=FSM_kz_passport.date_of_birth)
async def kz_pass_date_of_birth_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.last_name.set()
        await kz_pass_last_name_en_input(message, state)
    else:
        date_of_birth = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        format = "%d.%m.%Y"
        try:
            date_of_birth = datetime.strptime(message.text, format).strftime(format)
        except ValueError:
            pass

        if date_of_birth is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите дату в правильном формате.\n"
                    "<i>Формат ввода: дд.мм.гггг</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_date_of_birth,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(date_of_birth=date_of_birth.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_place_of_birth_input(message, state)


async def kz_pass_place_of_birth_input(message: types.Message, state: FSMContext):
    await state.update_data(place_of_birth=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите место рождения✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_place_of_birth,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.place_of_birth.set()


@dp.message_handler(state=FSM_kz_passport.place_of_birth)
async def kz_pass_place_of_birth_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.last_name_en.set()
        await kz_pass_date_of_birth_input(message, state)
    else:
        place_of_birth = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if message.text.lower() in countries:
            place_of_birth = message.text
        if place_of_birth is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите название страны на английском.\n"
                    "Или выберите из предложенных вариантов",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_place_of_birth,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(place_of_birth=place_of_birth.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_sex_input(message, state)


async def kz_pass_sex_input(message: types.Message, state: FSMContext):
    await state.update_data(sex=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Выберите пол✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_sex,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.sex.set()


@dp.message_handler(state=FSM_kz_passport.sex)
async def kz_pass_sex_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.date_of_birth.set()
        await kz_pass_place_of_birth_input(message, state)
    else:
        sex = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if message.text == "Мужской 🙎🏻‍♂️":
            sex = "M"
        elif message.text == "Женский 🙍🏻‍♀️":
            sex = "F"
        else:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите корректное значение.\n"
                    "<i>Формат ввода: 'Мужской' или 'Женский'</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_sex,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        if sex == "M" or sex == "F":
            await state.update_data(sex=sex)
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_nationality_input(message, state)


async def kz_pass_nationality_input(message: types.Message, state: FSMContext):
    await state.update_data(nationality=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    data = await state.get_data()
    if data["sex"] == "M":
        kb_kz_nationality = client_kb.kb_kz_nationality_m
    else:
        kb_kz_nationality = client_kb.kb_kz_nationality_f
    question_message = (
        await message.answer(
            "Выберите национальность✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=kb_kz_nationality,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.nationality.set()


@dp.message_handler(state=FSM_kz_passport.nationality)
async def kz_pass_nationality_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.place_of_birth.set()
        await kz_pass_sex_input(message, state)
    else:
        nationality = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if message.text in nationalities:
            nationality = message.text
        if nationality is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            if data["sex"] == "M":
                kb_kz_nationality = client_kb.kb_kz_nationality_m
            else:
                kb_kz_nationality = client_kb.kb_kz_nationality_f
            question_message = (
                await message.answer(
                    text="❌Выберите из предлженных вариантов.",
                    parse_mode=ParseMode.HTML,
                    reply_markup=kb_kz_nationality,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(nationality=nationality.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_authority_input(message, state)


async def kz_pass_authority_input(message: types.Message, state: FSMContext):
    await state.update_data(authority=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите орган выдачи пасспорта✏️\n",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_authority,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.authority.set()


@dp.message_handler(state=FSM_kz_passport.authority)
async def kz_pass_authority_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.sex.set()
        await kz_pass_nationality_input(message, state)
    else:
        authority = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if re.match("^[A-Za-z `]+$", message.text) is not None:
            authority = message.text
        if authority is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите имя в правильном формате.\n"
                    "<i>Формат ввода: только латинские буквы.</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_authority,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(authority=authority.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_issue_date_input(message, state)


async def kz_pass_issue_date_input(message: types.Message, state: FSMContext):
    await state.update_data(issue_date=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите дату выдачи✏️\n"
            "<i>Формат ввода: дд.мм.гггг</i>\n"
            "<i>Не раньше 01.01.2009</i>",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_issue_date,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.issue_date.set()


@dp.message_handler(state=FSM_kz_passport.issue_date)
async def kz_pass_issue_date_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.nationality.set()
        await kz_pass_authority_input(message, state)
    else:
        issue_date = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        format = "%d.%m.%Y"
        try:
            issue_date = datetime.strptime(message.text, format).strftime(format)
        except ValueError:
            pass

        input_date = None
        try:
            input_date = datetime.strptime(message.text, format)
        except:
            pass
        if input_date == None:
            input_date = "01.01.1800"
        exemplary_date = datetime.strptime("01.01.2009", format)

        if issue_date is None or input_date < exemplary_date:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите дату в правильном формате.\n"
                    "<i>Формат ввода: дд.мм.гггг</i>\n"
                    "<i>Не раньше 01.01.2009</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_issue_date,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(issue_date=issue_date.upper())
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_number_input(message, state)


async def kz_pass_number_input(message: types.Message, state: FSMContext):
    await state.update_data(pass_number=None)
    message_to_edit = await data_checkup(message.from_user.id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await message.answer(
            "Введите номер бланка паспорта✏️\n" "<i>Формат ввода: N12345678</i>",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_pass_number,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.pass_number.set()


@dp.message_handler(state=FSM_kz_passport.pass_number)
async def kz_pass_number_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.authority.set()
        await kz_pass_issue_date_input(message, state)
    else:
        pass_number = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if message.text == "Сгенерировать":
            pass_number = f"N{random.randrange(11111111,99999999)}"
        if re.match(r"^[N]\d{8}$", message.text) is not None:
            pass_number = message.text
        if pass_number is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите номер паспорта в правильном формате.\n"
                    "<i>Формат ввода: N12345678</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_pass_number,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(pass_number=pass_number)
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_jsn_input(message, state)


async def kz_pass_jsn_input(user_id: types.Message or int, state: FSMContext):
    if type(user_id) == types.Message:
        user_id = user_id.from_user.id
    await state.update_data(jsn=None)
    message_to_edit = await data_checkup(user_id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await dp.bot.send_message(
            chat_id=user_id,
            text="Введите ЖСН✏️\n" "<i>Формат ввода: 12 цифр</i>",
            parse_mode=ParseMode.HTML,
            reply_markup=client_kb.kb_kz_jsn,
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.jsn.set()


@dp.message_handler(state=FSM_kz_passport.jsn)
async def kz_pass_jsn_validate(message: types.Message, state: FSMContext):
    await delete_user_reply(message.from_user.id, message.message_id)
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.issue_date.set()
        await kz_pass_number_input(message, state)
    else:
        jsn = None
        data = await state.get_data()

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message not found in state data")

        if message.text == "Сгенерировать":
            jsn = get_jsn(data)

        if re.match(r"^\d{12}$", message.text) is not None:
            jsn = message.text
        if jsn is None:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await message.answer(
                    text="❌Введите ЖСН в правильном формате.\n"
                    "<i>Формат ввода: 12 цифр</i>",
                    parse_mode=ParseMode.HTML,
                    reply_markup=client_kb.kb_kz_jsn,
                )
            )["message_id"]
            await state.update_data(question_message=question_message)
        else:
            await state.update_data(jsn=jsn)
            if "edit_mode" in data:
                return await start_generate(message, state)
            await kz_pass_signature_input(message, state)


async def kz_pass_signature_input(user_id, state: FSMContext):
    await state.update_data(signature=None)
    if type(user_id) == types.Message:
        user_id = user_id.from_user.id
    message_to_edit = await data_checkup(user_id, state)
    await state.update_data(message_to_edit=message_to_edit)
    data = await state.get_data()

    process_generate_signature(user_id=user_id, data=data["sign"])

    kb = types.InlineKeyboardMarkup()
    kb.add(
        types.InlineKeyboardButton(text="1", callback_data="sig_1"),
        types.InlineKeyboardButton(text="2", callback_data="sig_2"),
        types.InlineKeyboardButton(text="3", callback_data="sig_3"),
    )
    kb.add(
        types.InlineKeyboardButton(text="4", callback_data="sig_4"),
        types.InlineKeyboardButton(text="5", callback_data="sig_5"),
        types.InlineKeyboardButton(text="6", callback_data="sig_6"),
    )
    kb.add(types.InlineKeyboardButton(text="Не ставить", callback_data="sig_0"))
    kb.add(types.InlineKeyboardButton(text="Назад ◀️", callback_data="sig_back"))

    photo_path = (
        "./files/kz/kz_passport/photo_usr/" + str(user_id) + "_choose_signature.png"
    )  # Замените на путь к вашей картинке
    with open(photo_path, "rb") as photo:
        question_message = (
            await dp.bot.send_photo(
                chat_id=user_id,
                photo=photo,
                caption="Выберите тип подписи:",
                reply_markup=kb,
            )
        )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.signature.set()


@dp.callback_query_handler(state=FSM_kz_passport.signature)
# lambda c: c.data.startswith("sig_"), state=FSM_kz_passport.signature
# )
async def kz_pass_signature_validate(
    callback_query: types.CallbackQuery, state: FSMContext
):
    if callback_query.data == "sig_back":
        data = await state.get_data()
        await dp.bot.delete_message(
            chat_id=callback_query.from_user.id, message_id=data["question_message"]
        )
        message_to_edit = await data_checkup(callback_query.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.signature.set()
        await kz_pass_jsn_input(callback_query.from_user.id, state)
    else:
        await dp.bot.answer_callback_query(callback_query.id)
        signature = int(callback_query.data.split("_")[1])
        await state.update_data(signature=int(signature))
        logger.info(f"Пользователь {callback_query.from_user.username} выбрал подпись.")

        data = await state.get_data()
        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=callback_query.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message_id not found in state data")
        if "edit_mode" in data:
            return await start_generate(callback_query.from_user.id, state)
        message_to_edit = await data_checkup(callback_query.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await kz_pass_photo_input(callback_query.from_user.id, state)


async def kz_pass_photo_input(user_id: int or types.Message, state: FSMContext):
    if type(user_id) == types.Message:
        user_id = user_id.from_user.id
    await state.update_data(photo=None)
    message_to_edit = await data_checkup(user_id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await dp.bot.send_message(
            chat_id=user_id,
            text="Загрузите фотографию 📷\n"
            " ❗️Требования к фотографии:\n"
            "➖ Фотография должна быть загружена без сжатия, нажимаем скрепку - файл - выбрать фото;\n"
            "➖ На фотографии должно быть видно плечи, глаза открыты;\n"
            "➖ Кадр должен быть в хорошем освещении;\n"
            "➖ Лицо должно быть под прямым углом к камере;\n"
            "Поддерживаемые форматы: JPG, JPEG, PNG ❗️",
        )
    )["message_id"]
    await state.update_data(question_message=question_message)
    await FSM_kz_passport.photo.set()


@dp.message_handler(
    content_types=types.ContentType.DOCUMENT, state=FSM_kz_passport.photo
)
async def process_photo(message: types.Message, state: FSMContext):
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.signature.set()
        await kz_pass_signature_input(message, state)
    else:
        data = await state.get_data()
        # Check if the message has a document
        # Check if the document is in the required format (PNG)
        file_info = await dp.bot.get_file(message.document.file_id)
        await message.delete()
        # if file_info.file_path.lower().endswith(".png"):
        # Download the document and save it as user_id.png

        photo_path = f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.png"
        await dp.bot.download_file(file_info.file_path, photo_path)

        out = remove_bg(Image.open(photo_path))
        out = out.convert("RGBA")
        out.save(f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.png")
        # Check if the image has an alpha channel
        img = Image.open(photo_path).convert("RGBA")
        if img.mode == "RGBA":
            # Process the photo in a separate thread
            thread = threading.Thread(
                target=process_passport_photo, args=(message.from_user.id,)
            )
            thread.start()
            # Проверка состояния потока каждую секунду с использованием asyncio.sleep
            while thread.is_alive():
                await asyncio.sleep(1)
            # Check if the processed photo exists
            processed_photo_path = (
                f"./files/kz/kz_passport/photo_usr/{message.from_user.id}_w.png"
            )
            if os.path.exists(processed_photo_path):
                data = await state.get_data()
                question_message = data.get("question_message")
                try:
                    await dp.bot.delete_message(
                        chat_id=message.chat.id, message_id=question_message
                    )
                    message_to_edit = await data_checkup(message.from_user.id, state)
                    await dp.bot.delete_message(
                        chat_id=message.chat.id, message_id=message_to_edit
                    )
                except:
                    pass
                await state.update_data(photo="Загружено")
                await kz_pass_size_input(message, state)
            else:
                await dp.bot.send_message(
                    chat_id=message.chat.id,
                    text="❌ Фотография не соответствует требованиям.\n"
                    "ИИ не смог обработать вашу фотографию.\n"
                    "Это происходит, если фото не соответствует требованиям, "
                    "проверьте что расширение файла PNG, "
                    "либо загрузите другое изображение.",
                )
        else:
            await dp.bot.send_message(
                chat_id=message.chat.id,
                text="❌ Фотография не соответствует требованиям.\n"
                "ИИ не смог обработать вашу фотографию.\n"
                "Это происходит, если фото не соответствует требованиям, "
                "проверьте что расширение файла PNG, "
                "либо загрузите другое изображение.",
            )
        # elif file_info.file_path.lower().endswith(".jpg"):
        #     # Download the document and save it as user_id.png
        #     photo_path = f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.png"
        #     await dp.bot.download_file(file_info.file_path, f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.jpg")
        #     # AI work
        #     out = remove_bg(Image.open(f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.jpg"))
        #     out.save(f'./files/kz/kz_passport/photo_usr/{message.from_user.id}.png')
        #     # Check if the image has an alpha channel
        #     img = Image.open(photo_path).convert("RGBA")
        #     if img.mode == "RGBA":
        #         # Process the photo in a separate thread
        #         thread = threading.Thread(
        #             target=process_passport_photo, args=(message.from_user.id,)
        #         )
        #         thread.start()
        #         # Проверка состояния потока каждую секунду с использованием asyncio.sleep
        #         while thread.is_alive():
        #             await asyncio.sleep(1)
        #         # Check if the processed photo exists
        #         processed_photo_path = (
        #             f"./files/kz/kz_passport/photo_usr/{message.from_user.id}_w.png"
        #         )
        #         if os.path.exists(processed_photo_path):
        #             data = await state.get_data()
        #             question_message = data.get("question_message")
        #             try:
        #                 await dp.bot.delete_message(
        #                     chat_id=message.chat.id, message_id=question_message
        #                 )
        #                 message_to_edit = await data_checkup(message.from_user.id, state)
        #                 await dp.bot.delete_message(
        #                     chat_id=message.chat.id, message_id=message_to_edit
        #                 )
        #             except:
        #                 pass
        #             await state.update_data(photo='Загружено')
        #             await kz_pass_size_input(message, state)
        #         else:
        #             await dp.bot.send_message(
        #                 chat_id=message.chat.id,
        #                 text="❌ Фотография не соответствует требованиям.\n"
        #                 "ИИ не смог обработать вашу фотографию.\n"
        #                 "Это происходит, если фото не соответствует требованиям, "
        #                 "проверьте что расширение файла PNG, "
        #                 "либо загрузите другое изображение.",
        #             )
        #     else:
        #         await dp.bot.send_message(
        #             chat_id=message.chat.id,
        #             text="❌ Фотография не соответствует требованиям.\n"
        #             "ИИ не смог обработать вашу фотографию.\n"
        #             "Это происходит, если фото не соответствует требованиям, "
        #             "проверьте что расширение файла PNG, "
        #             "либо загрузите другое изображение.",
        #         )

        # elif file_info.file_path.lower().endswith(".jpeg"):
        #     # Download the document and save it as user_id.png
        #     photo_path = f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.png"
        #     await dp.bot.download_file(file_info.file_path, f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.jpeg")
        #     # AI work
        #     out = remove_bg(Image.open(f"./files/kz/kz_passport/photo_usr/{message.from_user.id}.jpeg"))
        #     out.save(f'./files/kz/kz_passport/photo_usr/{message.from_user.id}.png')
        #     # Check if the image has an alpha channel
        #     img = Image.open(photo_path).convert("RGBA")
        #     if img.mode == "RGBA":
        #         # Process the photo in a separate thread
        #         thread = threading.Thread(
        #             target=process_passport_photo, args=(message.from_user.id,)
        #         )
        #         thread.start()
        #         # Проверка состояния потока каждую секунду с использованием asyncio.sleep
        #         while thread.is_alive():
        #             await asyncio.sleep(1)
        #         # Check if the processed photo exists
        #         processed_photo_path = (
        #             f"./files/kz/kz_passport/photo_usr/{message.from_user.id}_w.png"
        #         )
        #         if os.path.exists(processed_photo_path):
        #             data = await state.get_data()
        #             question_message = data.get("question_message")
        #             try:
        #                 await dp.bot.delete_message(
        #                     chat_id=message.chat.id, message_id=question_message
        #                 )
        #                 message_to_edit = await data_checkup(message.from_user.id, state)
        #                 await dp.bot.delete_message(
        #                     chat_id=message.chat.id, message_id=message_to_edit
        #                 )
        #             except:
        #                 pass
        #             await state.update_data(photo='Загружено')
        #             await kz_pass_size_input(message, state)

        #         else:
        #             await dp.bot.send_message(
        #                 chat_id=message.chat.id,
        #                 text="❌ Фотография не соответствует требованиям.\n"
        #                 "ИИ не смог обработать вашу фотографию.\n"
        #                 "Это происходит, если фото не соответствует требованиям, "
        #                 "проверьте что расширение файла PNG, "
        #                 "либо загрузите другое изображение.",
        #             )
        #     else:
        #         await dp.bot.send_message(
        #             chat_id=message.chat.id,
        #             text="❌ Фотография не соответствует требованиям.\n"
        #             "ИИ не смог обработать вашу фотографию.\n"
        #             "Это происходит, если фото не соответствует требованиям, "
        #             "проверьте что расширение файла PNG, "
        #             "либо загрузите другое изображение.",
        #         )
        # else:
        #     await dp.bot.send_message(
        #         chat_id=message.chat.id,
        #         text="❌ Фотография не соответствует требованиям.\n"
        #         "ИИ не смог обработать вашу фотографию.\n"
        #         "Это происходит, если фото не соответствует требованиям, "
        #         "проверьте что расширение файла PNG, "
        #         "либо загрузите другое изображение.",
        #     )


async def kz_pass_size_input(message: types.Message, state: FSMContext):
    user_id = message.from_user.id
    await state.update_data(size=None)
    message_to_edit = await data_checkup(user_id, state)
    await state.update_data(message_to_edit=message_to_edit)
    question_message = (
        await dp.bot.send_message(
            chat_id=user_id,
            text="Выберите размер документа",
            reply_markup=client_kb.kb_kz_pass_size,
        )
    )["message_id"]
    await FSM_kz_passport.size.set()
    await state.update_data(question_message=question_message)


@dp.message_handler(state=FSM_kz_passport.size)
async def kz_pass_size_validate(message: types.Message, state: FSMContext):
    if message.text == "Назад ◀️":
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await FSM_kz_passport.photo.set()
        await kz_pass_photo_input(message, state)
    else:
        size_type = None
        if message.text == "📃 На листе А4":
            size_type = "A4"
        elif message.text == "🪪 В размер документа":
            size_type = "DOC"
        elif message.text == "🖼 На фоне":
            size_type = "На фоне"
        else:
            message_to_edit = await data_checkup(message.from_user.id, state)
            await state.update_data(message_to_edit=message_to_edit)
            question_message = (
                await dp.bot.send_message(
                    chat_id=message.from_user.id,
                    text="Выберите размер документа",
                    reply_markup=client_kb.kb_kz_pass_size,
                )
            )["message_id"]
            return await state.update_data(question_message=question_message)
        await message.delete()
        await state.update_data(size=size_type)
        data = await state.get_data()
        logger.info(f"Пользователь {message.from_user.id} выбрал размер документа")

        if "question_message" in data:
            await dp.bot.delete_message(
                chat_id=message.from_user.id, message_id=data["question_message"]
            )
        else:
            logger.warning("question_message_id not found in state data")
        if "edit_mode" in data:
            return await start_generate(message, state)
        message_to_edit = await data_checkup(message.from_user.id, state)
        await state.update_data(message_to_edit=message_to_edit)
        await start_generate(message, state)


async def start_generate(user_id: types.Message or int, state: FSMContext):
    if type(user_id) == types.Message:
        user_id = user_id.from_user.id
    message_to_delete = await dp.bot.send_message(
        chat_id=user_id, text="Ожидайте генерируется..."
    )
    data = await state.get_data()
    result_queue = queue.Queue()
    thread = threading.Thread(target=kz_pass_generate, args=(data, result_queue))
    thread.start()
    thread.join()
    result = result_queue.get()
    if "Error" in result:
        await dp.bot.send_message(
            chat_id=user_id, text="❌Ошибка при генерации изображения"
        )
        # await dp.bot.send_message(chat_id=363661148, text=result)
    else:
        await dp.bot.delete_message(
            chat_id=user_id, message_id=message_to_delete["message_id"]
        )
        # with open(result + "_wm.png", "rb") as photo:
        with open(
            f"./files/kz/kz_passport/photo_usr/{user_id}_completed_wm.png", "rb"
        ) as doc_wm:
            await dp.bot.send_document(chat_id=user_id, document=doc_wm)
            await dp.bot.send_message(
                chat_id=user_id,
                text="Завершите покупку документа.",
                reply_markup=client_kb.kb_kz_finish,
            )
        await state.reset_state(with_data=False)


@dp.callback_query_handler(text="kz_change_pass", state="*")
async def kz_change_pass(call: types.CallbackQuery, state: FSMContext):
    kb = types.ReplyKeyboardMarkup(one_time_keyboard=True)
    await call.message.delete()
    for field in data_fields:
        kb.add(types.KeyboardButton(text=f'{field["title"]}'))
    message_to_edit = (
        await call.message.answer(
            text="Выберите параметр, который хотите изменить", reply_markup=kb
        )
    )["message_id"]
    await state.update_data(message_to_edit=message_to_edit)
    await FSM_kz_passport.edit.set()


@dp.message_handler(state=FSM_kz_passport.edit)
async def edit_field(message: types.Message, state: FSMContext):
    data = await state.get_data()
    await dp.bot.delete_message(
        chat_id=message.from_user.id, message_id=data["message_to_edit"]
    )
    await message.delete()
    key = None
    for field in data_fields:
        if field["title"] == message.text:
            key = field["key"]
    if key is None:
        return await message.answer("❌Выберите параметр через клавиатуру")
    await state.update_data(edit_mode=1)
    func_name = key
    func_name_with_input = "kz_pass_" + func_name + "_input"

    # Получаем ссылку на функцию с использованием имени и вызываем ее
    function_to_call = globals()[func_name_with_input]
    await function_to_call(message, state)


@dp.callback_query_handler(text="kz_pay_pass", state="*")
async def command_pay_kz_pass(callback: types.CallbackQuery, state: FSMContext):
    await state.finish()
    await state.update_data(pay_country="kz_pass")
    await client.command_promo_quest(callback, state)


# @dp.callback_query_handler(text="kz_exit_pass", state="*")
# async def command_exit_kz_pass(callback: types.CallbackQuery, state: FSMContext):
#     await dp.bot.send_message(chat_id=callback.from_user.id, text='Выход')
#     await state.finish()
